************************************************
* Sleep Project - Pedro Bessone, Gautam Rao, Heather Schofield, Frank Schilbach, and Mattie Toma
* Purpose: Master do-file. Runs a push-button replication of "The Economic Consequences of Increasing Sleep Among the Urban Poor"
* Last edited: 07 May 2021
************************************************
/* Instructions: .
	1. Set the path to this replication package in the areas marked "SET DIRECTORY HERE" (Lines 19 and 44 or 54)
	2. Set the globals "RI" and "reps;" the first takes 1 if you would like to replicate the randomization inference and 0 if not. The second sets the number of replications for the RI.
	3. Run this do-file 
*/
****************************************************************************

*** Step 1: setup
	clear all
	set more off
	version 16.1
	
	//Setting up globals
	global wd "" //SET DIRECTORY HERE
	cd "$wd"
	
	global s "Scripts"
	global sm "Scripts/Main_paper"
	global sa "Scripts/Appendix"
	global d "Datasets"
	global om "Output/Main_paper"
	global oa "Output/Appendix"
	
	global RI = 0 //Keep at 1 if you would like to replicate the randomization inference p-value adjustments; change to 0 (or other) if not. Note: if you would not like to run R files from the master, you will need to set this to 0.
	global reps = 10000 //Set the number of RI iterations; 10,000 are required to perfectly replicate the paper tables
	
* Step 2: Installing ssc commands necessary to run some chunks
	cap ssc install reghdfe
	cap ssc install distinct
	cap ssc install coefplot
	cap ssc install mdesc
	cap ssc install ivreghdfe
	cap ssc install qqvalue
	
* Step 3: Preparing the R environment. NOTE: the below will not run if you delete the lines in the if/else statements after "END_OF_R"
	
	if "`c(os)'"=="MacOSX" | "`c(os)'"=="UNIX" {
    rsource, terminator(END_OF_R) rpath("/usr/local/bin/R") roptions(`"--vanilla"')  maxlines(2)
	setwd(""); // SET DIRECTORY HERE
	q();
	END_OF_R

	

}

else {  // windows
    rsource terminator(END_OF_R) rpath("/usr/local/bin/R") roptions(`"--vanilla"')  maxlines(2)
	setwd(""); // SET DIRECTORY HERE
	q();
	END_OF_R
	
	
	
}

* Step 4: Running do-files

** Main paper tables
forvalues x=1/4{
	do "$sm/Table_`x'"
	cd "$wd"
}

** Main paper figures
foreach x of numlist 1/2 4/5{
	do "$sm/Figure_`x'"
	cd "$wd"
}


** Appendix tables
foreach x of numlist 1/2 4/17 19/22{
	do "$sa/Table_A`x'"
	cd "$wd"
}

** Appendix figures
foreach x of numlist 1 4/8{
	do "$sa/Figure_A`x'"
	cd "$wd"
}

* Step 5: Running R scripts:

if "`c(os)'"=="MacOSX" | "`c(os)'"=="UNIX" {
    rsource using Scripts/Main_paper/Figure_6.R, rpath("/usr/local/bin/R") roptions(`"--vanilla"') //Figure 6
	 rsource using Scripts/Appendix/Figure_A2.R, rpath("/usr/local/bin/R") roptions(`"--vanilla"') //Figure A2
    rsource using Scripts/Appendix/Figure_A9.R, rpath("/usr/local/bin/R") roptions(`"--vanilla"') //Figure A9
    rsource using Scripts/Appendix/Table_A18.R, rpath("/usr/local/bin/R") roptions(`"--vanilla"') //Table A18

}
else {  // windows
    rsource using Scripts/Main_paper/Figure_6.R, rpath(`"c:\r\R-3.5.1\bin\Rterm.exe"') roptions(`"--vanilla"')  // change version number, if necessary
	    rsource using Scripts/Appendix/Figure_A2.R, rpath(`"c:\r\R-3.5.1\bin\Rterm.exe"') roptions(`"--vanilla"')
		rsource using Scripts/Appendix/Figure_A9.R, rpath(`"c:\r\R-3.5.1\bin\Rterm.exe"') roptions(`"--vanilla"')
	    rsource using Scripts/Appendix/Table_A18.R, rpath(`"c:\r\R-3.5.1\bin\Rterm.exe"') roptions(`"--vanilla"')
}



